package com.shareppy.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Holiday implements Comparable<Holiday>{

	public static final Locale locale = Locale.getDefault();

	public static final SimpleDateFormat wikipedia_formatter = new SimpleDateFormat(
			getFormat(locale), locale);

	int year;
	int month;
	int day;
	boolean fixed;
	String description;

	public Holiday(int year, int month, int day) {
		this(year, month, day, false);
	}

	public Holiday(int year, int month, int day, String description) {
		this(year, month, day, false, description);
	}

	public Holiday(int year, int month, int day, boolean fixed) {
		this(year, month, day, fixed, null);
	}

	public Holiday(int year, int month, int day, boolean fixed,
			String description) {
		this.year = year;
		this.month = month;
		this.day = day;
		this.fixed = fixed;
		this.description = description;
	}

	public Holiday(Calendar calendar) {
		this(calendar, false);
	}

	public Holiday(Calendar calendar, boolean fixed) {
		fromCalendar(calendar, fixed);
	}

	private void fromCalendar(Calendar calendar) {
		fromCalendar(calendar, false);
	}

	private void fromCalendar(Calendar calendar, boolean fixed) {
		this.year = calendar.get(Calendar.YEAR);
		this.month = calendar.get(Calendar.MONTH);
		this.day = calendar.get(Calendar.DATE);
		this.fixed = fixed;
	}

	public int normalize() {
		return month * 100 + day;
	}

	public long fullNormalize() {
		return year * 10000 + month * 100 + day;
	}

	@Override
	public String toString() {
		return String.valueOf(toWikipediaLink());
	}

	public Calendar toCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	public String toWikipediaLink() {
		return "http://" + locale.getLanguage() + ".wikipedia.org/wiki/"
				+ wikipedia_formatter.format(toCalendar().getTime());
//		return toCalendar().getTime().toString();
	}

	public static String getFormat(Locale locale) {
		if (locale.equals(Locale.US)) {
			return "MMMM'_'dd";
		}
		return "dd'_de_'MMMM";
	}

	public Holiday toNextMonday() {
		if (!fixed) {
			Calendar calendar = toCalendar();
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			if (dayOfWeek != Calendar.MONDAY) {
				calendar.add(Calendar.DATE, (9 - dayOfWeek) % 7);
				fromCalendar(calendar);
			}
		}
		return this;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Holiday){
			Holiday otherObj = (Holiday) obj;
			return fullNormalize() == otherObj.fullNormalize();
		}
		return super.equals(obj);
	}
	
	@Override
	public int compareTo(Holiday o) {
		return toCalendar().compareTo(o.toCalendar());
	}
	
	@Override
	public int hashCode() {
		return  Long.valueOf(fullNormalize()).hashCode();
	} 
	
}